﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PJAA_Inventory_and_AR___Prototype
{
    public partial class Add_New_Item : Form
    {
        private SQLMaster master;
        private string products;

        public Add_New_Item()
        {
            //
            //added this block to make the form transparent
            //
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.BackColor = Color.Transparent;

            InitializeComponent();

            master = new SQLMaster();

            loadProductTypeComboBox();
            loadComponentTypeComboBox();
            addFirstEmptyValue();

        }

        public void loadProductTypeComboBox()
        { 
            List<object[]> prodtypes= new List<object[]>();//create new arraylist of arrays
            prodtypes= master.loadLookupCategoriesVER2();// poplulate arry list with the result of the sql query

            foreach(object[] arrayList in prodtypes)
            {
                comboBox_itemType.Items.Add(arrayList[0]);
            }
        }

        private void loadComponentTypeComboBox()
        {
            List<object[]> component = new List<object[]>();//creatte new arraylist of arrays
            component = master.loadComponentTypeColumn_inv();// poplulate arry list with the result of the sql query

            foreach (object[] arrayList in component)
            {
                comboBox_component.Items.Add(arrayList[0]);
            }
        }

         private void PopulateCombobox(ComboBox ComboBoxtoPopulate, List<object[]> arrayOfInventoryType)
        {
            ComboBoxtoPopulate.Refresh();

            foreach (object[] strArray in arrayOfInventoryType)
            {
                //DI KO ALAM KUNG BAKIT MAY FORLOOP DITO... @_@

                //for (int i = 0; i <= strArray.Length - 1; i++)
                //{
                    //string valueInsideArray = strArray[i].ToString();
                    ComboBoxtoPopulate.Items.Add(strArray[0]);//.ToString().Substring(6,valueInsideArray.Length-6));
                //}
            }
        }

        //loadpics combobox
         private void loadExtraCombobox()
         {
             List<object[]> prodtypes = new List<object[]>();//create new arraylist of arrays
             prodtypes = master.loadLookupCategoriesVER2();// poplulate arry list with the result of the sql query


             PopulateCombobox(comboBox_prod1, prodtypes);
             PopulateCombobox(comboBox_prod2, prodtypes);
             PopulateCombobox(comboBox_prod3, prodtypes);
             PopulateCombobox(comboBox_prod4, prodtypes);
             PopulateCombobox(comboBox_prod5, prodtypes);
         }


        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void AddNewItem_AddItem_Btn_Click(object sender, EventArgs e)
        {
            decimal unitCost=0m;
            if (AddNewItem_ItemName_TxtBox.Text.Equals("") || AddNewItem_ItemName_TxtBox.Text.Equals(null) ||
                comboBox_itemType.Text.Equals("")||comboBox_itemType.Text.Equals(null)||
                (checkBox_other.Checked.Equals(true) || maskedTextBox1.Text.Equals(".")))
            {//catch any blank fields
                MessageBox.Show("Please complete all the data", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                try
                {//tries to convert the unitcost
                    unitCost = Convert.ToDecimal(maskedTextBox1.Text.ToString());
                }
                catch
                {
                    MessageBox.Show("Do not leave the unit cost blank", "", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            else
            {
                unitCost = Convert.ToDecimal(maskedTextBox1.Text.ToString());
                string productType = comboBox_itemType.Text;

                if (groupBox1.Visible == true)
                {
                    createMultipleProducts(unitCost);
                }

                else
                {
                    master.addInventory(AddNewItem_ItemName_TxtBox.Text, productType, comboBox_component.Text, unitCost, Convert.ToInt16(numericUpDown_Qty.Value));
                }

                MessageBox.Show("Successfully added [" + AddNewItem_ItemName_TxtBox.Text + "] to the inventory list", "Success", MessageBoxButtons.OK, MessageBoxIcon.Information);

                if (MessageBox.Show("Do you want to add another item?", "Item addition", MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    AddNewItem_ItemName_TxtBox.Clear();
                    comboBox_itemType.Text = "";
                    comboBox_component.Text = "";
                
                    checkBox_other.Checked = false;
                    maskedTextBox1.Clear();
                    numericUpDown_Qty.Value = 0;
                }
                else
                {
                    this.Close();
                }

            }
        }

        private void createMultipleProducts(decimal unitCost)
        {
            products += comboBox_itemType.Text;
            
            if (comboBox_prod1.SelectedIndex > 1)
            {
                products+= "/"+comboBox_prod1.Text ;
            }

            if (comboBox_prod2.SelectedIndex > 1)
            {
                products += "/" + comboBox_prod2.Text;
            }
            if (comboBox_prod3.SelectedIndex > 1)
            {
                products += "/" + comboBox_prod3.Text;
            }
            if (comboBox_prod4.SelectedIndex > 1)
            {
                products += "/" + comboBox_prod4.Text;
            }
            if (comboBox_prod5.SelectedIndex > 1)
            {
                products += "/" + comboBox_prod5.Text;
            }

            

            master.addInventory(AddNewItem_ItemName_TxtBox.Text, products, comboBox_component.Text, unitCost, Convert.ToInt16(numericUpDown_Qty.Value));
        }

        private void addFirstEmptyValue()
        {
            comboBox_prod1.Items.Add("");
            comboBox_prod2.Items.Add("");
            comboBox_prod3.Items.Add("");
            comboBox_prod4.Items.Add("");
            comboBox_prod5.Items.Add("");
        }
        private void checkBox_other_CheckedChanged(object sender, EventArgs e)
        {
            if (checkBox_other.Checked)
            {
                //CALL THE WINDOW
                AddNewProductForLookup addnewprodforlookup = new AddNewProductForLookup(this);
                addnewprodforlookup.ShowDialog();

                checkBox_other.Checked = false;
            }
            else
            {
               
            }
        }


        private void maskedTextBox1_KeyPress(object sender, KeyPressEventArgs e)
        { 
            if (Char.IsDigit(e.KeyChar))
            {
                // Digits are OK
            }

            else if (e.KeyChar == '\b')
            {
                // Backspace key is OK      
            }
            else
            {
                // Consume this invalid key and beep
                e.Handled = true;
                //    MessageBeep();
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            loadExtraCombobox();
            groupBox1.Visible = true;

        }

        private void button2_Click_1(object sender, EventArgs e)
        {
            groupBox1.Visible = false;
        }


    





    }
}
